<template>
	<view class="wrap bg1">
		<view class="demos">
			<view class="content content1">
				<view class="box1">
					<p class="p1 one">
						<span class="moneyIcon">￥</span>
						<span :class="{ noneBorder: couponMoney != '' }" class="money">{{ couponMoney }}</span>
						{{name}}
					</p>
					<p class="p2 one">
						实付订单金额满
						<span :class="{ noneBorder: money != '' }" class="moneynum">{{ money }}</span>
						可用
					</p>
					<p class="p3 one">
						有效期：
						<span :class="{ noneBorder: time != '' }" class="time">{{ time }}</span>
					</p>
				</view>
				<view class="box2">
					<view class="btn">去使用</view>
				</view>
			</view>
		</view>
		<view class=" bgw fs-30 p30">
			<view class="list ">
				<view>优惠金额(元) :</view>
				<view><input @blur="couponMoneys" maxlength="7" v-model="couponMoney" type="number" placeholder="请输入优惠金额" /></view>
			</view>
			<view class="list">
				<view>消费金额(元) :</view>
				<view><input @blur="moneys" maxlength="7" v-model="money" type="number" placeholder="请输入消费金额" /></view>
			</view>
			<view @tap="timeTab()" class="list">
				<view>有效期 :</view>
				<view><input type="text" v-model="time" placeholder="请选择时间段" disabled /></view>
				<image class="go" src="../../static/image/go1.png" mode=""></image>
			</view>
			<view class="list">
				<view>优惠券数量(张) :</view>
				<view><input @blur="sums" v-model="sum" maxlength="6" type="number" placeholder="请输入数量" /></view>
			</view>
			<view class="list list2">
				<view>分类标签 :</view>
				<view style="padding-top: 34rpx;"><textarea v-model="txt" maxlength="50" placeholder="方便用户查找，多个标签用逗号隔开如水果，蔬菜，烟酒" /></view>
			</view>
		</view>

		<view @tap="checking()" class="bttoom p30"><button class="btnbg">创建优惠券</button></view>
		<w-pickers v-if="timestate === 1" mode="range" startDate="2019" :endYear="timeend" :defaultVal="rangeDval" :current="false" @confirm="onConfirm" ref="range" themeColor="#FF663D"></w-pickers>
	</view>
</template>

<script>
import wPickers from '@/components/w-pickers/w-picker.vue';
import { insertCoupon } from '@/api/api.js';
export default {
	components: {
		wPickers
	},
	data() {
		return {
			shakeState:0,
			couponMoney: '',
			money: '',
			sum: '',
			txt: '',
			time: '',
			time1:'',
			time2:'',
			rangeDval:['2020-04-13', '2020-12-18'],
			timestart: '',
			timeend: '',
			name:'',
			timestate:0
		};
	},
	onLoad(e) {
		this.name = e.name
	}, 
	mounted() {  
		var day2 = new Date();
		 day2.setTime(day2.getTime());
		 let mounth = (day2.getMonth()+1)
		 if(mounth < 10){
			 mounth='0'+mounth
		 } 
		   let days = day2.getDate()
		  if(days < 10){
		  			 days='0'+days
		  }  
		 var s1 = day2.getFullYear()+"-" + mounth+ "-" +days ;
		
		 var s2 = (day2.getFullYear()+1)+"-" + mounth + "-" +days; 
		 this.timestart = day2.getFullYear()
		 this.timeend = (day2.getFullYear()+5) 
		this.rangeDval[0]=s1
		this.rangeDval[1]=s2 
		this.timestate = 1 
	}, 
	methods: {
		checking(e) {
			var day2 = new Date();
			this.couponMoney = Math.round(this.couponMoney * 100) / 100;
			this.money = Math.round(this.money * 100) / 100;
			let mounth = (day2.getMonth()+1)
			if(mounth < 10){
						 mounth='0'+mounth
			} 
			var s1 = day2.getFullYear()+"-" + mounth+ "-" + day2.getDate();
			try {
				if (this.couponMoney <1 ||  this.sum > 10000) throw '优惠金额大于1';
				if (this.money <1 || this.sum > 10000 ) throw '消费金额大于1';
				if (this.time == '') throw '请输入有效期';
				if (this.time1 == this.time2) throw '有效期不正确'; 
				if (this.sum == '') throw '请输入数量';
				if (this.sum < 1 ||  this.sum > 1000000) throw '优惠券数量错误';
				if (this.txt == '') throw '请输入分类标签';
			} catch (e) {
				uni.showToast({
					title: e,
					icon: 'none'
				});
				return;
			}
			if(this.shakeState == 0){
				this.shakeState = 1
				this.send();		
			} 
		},
		async send() {
			let data = {
				withAmount: this.money,
				label: this.txt,
				total: this.sum,
				colour: 0,
				validEndTime: this.time2,
				validStartTime: this.time1,
				amount: this.couponMoney
			};
			let state = await insertCoupon(data);
			if (state.code == 0) {
				uni.navigateBack();
			}else{
				this.shakeState = 0
			}
		},
		onConfirm(e) {
			var day2 = new Date();
			this.time = e.result;
			this.time1 = e.from;
			this.time2 = e.to;
			let mounth = (day2.getMonth()+1)
			if(mounth < 10){
				  mounth='0'+mounth
			} 
			var s1 = day2.getFullYear()+"-" + mounth+ "-" + day2.getDate(); 
			if(this.time1 == this.time2){
				uni.showToast({
					title:'有效期不正确',
					icon:'none'
				})
				setTimeout(()=>{
					this.timeTab()
				},1000)
			}
		},
		timeTab() {
			this.$refs['range'].show();
		},
		sums() {
			this.sum = Math.floor(this.sum);
		},
		moneys() {
			this.money = Math.round(this.money * 100) / 100;
			if (this.money < 1 || this.money > 10000 ) {
				this.money = '';
				uni.showToast({
					title: '消费金额  1-9999',
					icon: 'none'
				});
			}
		},
		couponMoneys() {
			this.couponMoney = Math.round(this.couponMoney * 100) / 100;
			if (this.couponMoney < 1 || this.couponMoney > 10000 ) {
				this.couponMoney = '';
				uni.showToast({
					title: '优惠金额 1-9999',
					icon: 'none'
				});
			}
		}
	},
	computed: {}
};
</script>

<style lang="less" scoped>
@import './index.less';
</style>
